//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "NorthAmericaCorpDefaultKey.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Currency.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Experimental/Credit/DefaultType.h>
#include <gen/QL/Experimental/Credit/DefaultProbKey.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::CNorthAmericaCorpDefaultKey (Cephei::QL::ICurrency^ currency, QL::Experimental::Credit::SeniorityEnum sen, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ graceFailureToPay, Microsoft::FSharp::Core::FSharpOption<Double>^ amountFailure, Microsoft::FSharp::Core::FSharpOption<QL::Experimental::Credit::Restructuring::TypeEnum>^ resType) : CDefaultProbKey(CNorthAmericaCorpDefaultKey::typeid)
{
    CCurrency^ _Ccurrency;
    CPeriod^ _CgraceFailureToPay;
    try
    {
#ifdef HANDLE
        _phNorthAmericaCorpDefaultKey = NULL;
#endif
        _Ccurrency = safe_cast<CCurrency^> (currency);
        _Ccurrency->Lock();
        QuantLib::Currency& _currency = static_cast<QuantLib::Currency&> (_Ccurrency->GetReference ()); 
        QuantLib::Seniority _sen = (QuantLib::Seniority)sen ;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (graceFailureToPay))
        {
            _CgraceFailureToPay = safe_cast<CPeriod^> (graceFailureToPay->Value);
            _CgraceFailureToPay->Lock();
        }
        QuantLib::Period _graceFailureToPay = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (graceFailureToPay) ? static_cast<QuantLib::Period> (_CgraceFailureToPay->GetReference ()) : QuantLib::Period(30, Days)); //1
        QuantLib::Real _amountFailure = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (amountFailure) ? (QuantLib::Real)ValueHelper::Convert (amountFailure->Value) : 1.e6); //4
        QuantLib::Restructuring::Type _resType = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Experimental::Credit::Restructuring::TypeEnum>::IsSome::get (resType) ? (QuantLib::Restructuring::Type)resType->Value : QuantLib::Restructuring::Type::CR); //10
        _ppNorthAmericaCorpDefaultKey = new boost::shared_ptr<QuantLib::NorthAmericaCorpDefaultKey> (new QuantLib::NorthAmericaCorpDefaultKey ( _currency,  _sen,  _graceFailureToPay,  _amountFailure,  _resType ));
        SetDefaultProbKey (boost::dynamic_pointer_cast<QuantLib::DefaultProbKey> (*_ppNorthAmericaCorpDefaultKey));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccurrency != nullptr) _Ccurrency->Unlock();
        if (_CgraceFailureToPay != nullptr) _CgraceFailureToPay->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::CNorthAmericaCorpDefaultKey (boost::shared_ptr<QuantLib::NorthAmericaCorpDefaultKey>& childNative, Object^ owner) : CDefaultProbKey(CNorthAmericaCorpDefaultKey::typeid)
{
#ifdef HANDLE
	_phNorthAmericaCorpDefaultKey = NULL;
#endif
	_ppNorthAmericaCorpDefaultKey = &childNative;
    _ppDefaultProbKey = new boost::shared_ptr<QuantLib::DefaultProbKey> (boost::dynamic_pointer_cast<QuantLib::DefaultProbKey> (*_ppNorthAmericaCorpDefaultKey));
}
Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::CNorthAmericaCorpDefaultKey (QuantLib::NorthAmericaCorpDefaultKey& childNative, Object^ owner) : CDefaultProbKey(CNorthAmericaCorpDefaultKey::typeid)
{
#ifdef HANDLE
	_phNorthAmericaCorpDefaultKey = NULL;
#endif
	_ppNorthAmericaCorpDefaultKey = new boost::shared_ptr<QuantLib::NorthAmericaCorpDefaultKey> (&childNative);
    _ppDefaultProbKey = new boost::shared_ptr<QuantLib::DefaultProbKey> (boost::dynamic_pointer_cast<QuantLib::DefaultProbKey> (*_ppNorthAmericaCorpDefaultKey));
    _NorthAmericaCorpDefaultKeyOwner = owner;
    _DefaultProbKeyOwner = owner;
}

Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::CNorthAmericaCorpDefaultKey (CNorthAmericaCorpDefaultKey^ copy) : CDefaultProbKey(CNorthAmericaCorpDefaultKey::typeid)
{
#ifdef HANDLE
	_phNorthAmericaCorpDefaultKey = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppNorthAmericaCorpDefaultKey = new boost::shared_ptr<QuantLib::NorthAmericaCorpDefaultKey> (copy->GetShared());
        _ppDefaultProbKey = new boost::shared_ptr<QuantLib::DefaultProbKey> (boost::dynamic_pointer_cast<QuantLib::DefaultProbKey> (*_ppNorthAmericaCorpDefaultKey));
    }
}
Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::CNorthAmericaCorpDefaultKey (PLATFORM::Type^ t) : CDefaultProbKey(CNorthAmericaCorpDefaultKey::typeid)
{
#ifdef HANDLE
	_phNorthAmericaCorpDefaultKey = NULL;
#endif
	if (!t->IsSubclassOf(CNorthAmericaCorpDefaultKey::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::CNorthAmericaCorpDefaultKey (QuantLib::Handle<QuantLib::NorthAmericaCorpDefaultKey>& childNative, Object^ owner)  : CDefaultProbKey(CNorthAmericaCorpDefaultKey::typeid)
{
	_phNorthAmericaCorpDefaultKey = &childNative;
	_ppNorthAmericaCorpDefaultKey = &static_cast<boost::shared_ptr<QuantLib::NorthAmericaCorpDefaultKey>>(childNative.currentLink());
    _ppDefaultProbKey = new boost::shared_ptr<QuantLib::DefaultProbKey> (boost::dynamic_pointer_cast<QuantLib::DefaultProbKey> (*_ppNorthAmericaCorpDefaultKey));
    _NorthAmericaCorpDefaultKeyOwner = owner;
}
Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::CNorthAmericaCorpDefaultKey (QuantLib::Handle<QuantLib::NorthAmericaCorpDefaultKey> childNative)  : CDefaultProbKey(CNorthAmericaCorpDefaultKey::typeid)
{
	_phNorthAmericaCorpDefaultKey = &childNative;
	_ppNorthAmericaCorpDefaultKey = &static_cast<boost::shared_ptr<QuantLib::NorthAmericaCorpDefaultKey>>(childNative.currentLink());
    _ppDefaultProbKey = new boost::shared_ptr<QuantLib::DefaultProbKey> (boost::dynamic_pointer_cast<QuantLib::DefaultProbKey> (*_ppNorthAmericaCorpDefaultKey));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::CNorthAmericaCorpDefaultKey (QuantLib::NorthAmericaCorpDefaultKey childNative)  : CDefaultProbKey(CNorthAmericaCorpDefaultKey::typeid)
{
#ifdef HANDLE
	_phNorthAmericaCorpDefaultKey = NULL;
#endif
	_ppNorthAmericaCorpDefaultKey = new boost::shared_ptr<QuantLib::NorthAmericaCorpDefaultKey> (new QuantLib::NorthAmericaCorpDefaultKey (childNative));
    _ppDefaultProbKey = new boost::shared_ptr<QuantLib::DefaultProbKey> (boost::dynamic_pointer_cast<QuantLib::DefaultProbKey> (*_ppNorthAmericaCorpDefaultKey));
}
#endif

Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::~CNorthAmericaCorpDefaultKey ()
{
    if (_ppNorthAmericaCorpDefaultKey != NULL)
    {
	    delete _ppNorthAmericaCorpDefaultKey;
        _ppNorthAmericaCorpDefaultKey = NULL;
    }
}
Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::!CNorthAmericaCorpDefaultKey ()
{
    if (_ppNorthAmericaCorpDefaultKey != NULL)
    {
	    delete _ppNorthAmericaCorpDefaultKey;
    }
}
QuantLib::NorthAmericaCorpDefaultKey& Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::GetReference ()
{
    if (_ppNorthAmericaCorpDefaultKey == NULL) throw REFNEW NativeNullException ();
	return **_ppNorthAmericaCorpDefaultKey;
}
boost::shared_ptr<QuantLib::NorthAmericaCorpDefaultKey>& Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::GetShared ()
{
    if (_ppNorthAmericaCorpDefaultKey == NULL) throw REFNEW NativeNullException ();
	return *_ppNorthAmericaCorpDefaultKey;
}
QuantLib::NorthAmericaCorpDefaultKey* Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::GetPointer ()
{
    if (_ppNorthAmericaCorpDefaultKey == NULL) throw REFNEW NativeNullException ();
	return &**_ppNorthAmericaCorpDefaultKey;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::NorthAmericaCorpDefaultKey>& Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::GetHandle ()
{
	if (_phNorthAmericaCorpDefaultKey == NULL)
	{
		_phNorthAmericaCorpDefaultKey = new Handle<QuantLib::NorthAmericaCorpDefaultKey> (*_ppNorthAmericaCorpDefaultKey);
	}
	return *_phNorthAmericaCorpDefaultKey;
}
#endif
bool Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey::HasNative () 
{
	return (_ppNorthAmericaCorpDefaultKey != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::INorthAmericaCorpDefaultKey^ Cephei::QL::Experimental::Credit::CNorthAmericaCorpDefaultKey_Factory::Create (Cephei::QL::ICurrency^ currency, QL::Experimental::Credit::SeniorityEnum sen, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ graceFailureToPay, Microsoft::FSharp::Core::FSharpOption<Double>^ amountFailure, Microsoft::FSharp::Core::FSharpOption<QL::Experimental::Credit::Restructuring::TypeEnum>^ resType)
{
    return REFNEW CNorthAmericaCorpDefaultKey ( currency,  sen,  graceFailureToPay,  amountFailure,  resType);
}
